package com.interest.modules.zen.service.impl;

import com.interest.modules.zen.dao.ZenMapper;
import com.interest.modules.zen.entity.Zen;
import com.interest.modules.zen.service.ZenService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * Created by yangfeng on 16-11-9.
 */
@Service("zenService")
public class ZenServiceImpl implements ZenService {
    Logger logger = Logger.getLogger(ZenServiceImpl.class);
    @Autowired
    ZenMapper zenMapper;

    @Override
    public Zen getZen() {
//        return generateZen();
        List<Zen> zens = zenMapper.selectList();
        if (null == zens || zens.isEmpty()) {
            return generateZen();
        }

        int i = new Random().nextInt(zens.size());
        logger.info("random index = " + i);
        return zens.get(i);
    }

    private Zen generateZen() {
        Zen zen = new Zen();
        zen.setContent("Software is hard.");
        return zen;
    }
}
